package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Receiving_address;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.Receiving_addressService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: xsc_hunter
 * @Date: 2024/12/4) 上午11:23
 * @Version: 1.0
 */

@RestController
@RequestMapping("/receiving_address")
@AllArgsConstructor
@ResponseResult
public class Receiving_addressController {

    private final Receiving_addressService receivingAddressService;

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        PageEntity<Receiving_address> receivingAddressPage = receivingAddressService.findShopCartByPage(Integer.parseInt(current), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(receivingAddressPage);
    }

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String receiving_address = req.getParameter("receiving_address");
        String receiving_person = req.getParameter("receiving_person");
        String mobile_phone = req.getParameter("mobile_phone");
        String user_id = req.getParameter("user_id");
        int is_default = Integer.valueOf(req.getParameter("is_default"));
        Receiving_address receivingAddress = new Receiving_address(id,
                receiving_address,
                receiving_person,
                mobile_phone,
                user_id,
                is_default);
        Boolean save = receivingAddressService.save(receivingAddress);
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Receiving_address receivingAddress = receivingAddressService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(receivingAddress);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Receiving_address> receivingAddresses = receivingAddressService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(receivingAddresses);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean remove = receivingAddressService.remove(id);
        return ResponseVo.buildSuccess(remove);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String receiving_address = req.getParameter("receiving_address");
        String receiving_person = req.getParameter("receiving_person");
        String mobile_phone = req.getParameter("mobile_phone");
        String user_id = req.getParameter("user_id");
        int is_default = Integer.valueOf(req.getParameter("is_default"));

        Boolean update = receivingAddressService.update(new Receiving_address(id,
                receiving_address,
                receiving_person,
                mobile_phone,
                user_id,
                is_default));

        return ResponseVo.buildSuccess(update);
    }

}
